#import "LoadingView.h"
#import "AppDelegate.h"
#import "UIView-ViewFrameGeometry.h"
@interface LoadingView()
{
//    UIView * _bgView;
//    UIImageView * _bgImageView;
//    UIActivityIndicatorView * _avLoadingView;
//    UILabel * _lblTitle;
}
@end

@implementation LoadingView

@synthesize bgView = _bgView;
@synthesize bgImageView = _bgImageView;
@synthesize avLoadingView = _avLoadingView;
@synthesize lblTitle = _lblTitle;

- (void)setTitle:(NSString *)title {
    _lblTitle.text = title;
}

- (void)startLoading:(NSString *)title {

    _bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 110, 110)];
    [self addSubview:_bgView];
    
    _bgImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, _bgView.width, _bgView.height)];
    [_bgImageView setImage:[UIImage imageNamed:@"background_loading.png"]];
    [_bgView addSubview:_bgImageView];
    if(!title || [title isEqualToString:@""]){
        title=@"正在处理。。。";
    }
    _avLoadingView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    [_avLoadingView setFrame:CGRectMake((_bgView.width-25)/2.0, (_bgView.height/2.0-20)/2.0+5, 20, 20)];
    [_avLoadingView setContentMode:UIViewContentModeScaleToFill];
    [_bgImageView addSubview:_avLoadingView];
    
    _lblTitle = [[UILabel alloc] initWithFrame:CGRectMake(5, _bgView.height/2.0, _bgView.width-10, _bgView.height/2.0)];
    [_lblTitle setBackgroundColor:[UIColor clearColor]];
    [_lblTitle setTextColor:[UIColor whiteColor]];
    [_lblTitle setNumberOfLines:0];
    [_lblTitle setTextAlignment:NSTextAlignmentCenter];
    [_lblTitle setFont:[UIFont systemFontOfSize:14]];
    [_bgView addSubview:_lblTitle];
    
   
    
    
    AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    self.frame = delegate.window.bounds;
    _lblTitle.text = title;
    [_avLoadingView startAnimating];
    _bgView.center = delegate.window.center;

    [delegate.window addSubview:self];
}

- (void)startLoading:(NSString *)title :(CGPoint)center {
    
    _bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 110, 110)];
    [self addSubview:_bgView];
    
    _bgImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, _bgView.width, _bgView.height)];
    [_bgImageView setImage:[UIImage imageNamed:@"background_loading.png"]];
    [_bgView addSubview:_bgImageView];
    if(!title || [title isEqualToString:@""]){
        title=@"正在处理。。。";
    }
    _avLoadingView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    [_avLoadingView setFrame:CGRectMake((_bgView.width-25)/2.0, (_bgView.height/2.0-20)/2.0+5, 20, 20)];
    [_avLoadingView setContentMode:UIViewContentModeScaleToFill];
    [_bgImageView addSubview:_avLoadingView];
    
    _lblTitle = [[UILabel alloc] initWithFrame:CGRectMake(5, _bgView.height/2.0, _bgView.width-10, _bgView.height/2.0)];
    [_lblTitle setBackgroundColor:[UIColor clearColor]];
    [_lblTitle setTextColor:[UIColor whiteColor]];
    [_lblTitle setNumberOfLines:0];
    [_lblTitle setTextAlignment:NSTextAlignmentCenter];
    [_lblTitle setFont:[UIFont systemFontOfSize:14]];
    [_bgView addSubview:_lblTitle];
    
    
    
    
    AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    self.frame = delegate.window.bounds;
    _lblTitle.text = title;
    [_avLoadingView startAnimating];
    _bgView.center = center;
    
    [delegate.window addSubview:self];
}

- (void)stopLoading {
    [_avLoadingView stopAnimating];
    [self removeFromSuperview];
}

@end
